<template>
  <div class="dancePerformance">
    <x-header class="header" :left-options="{showBack: showHeaderBack,preventGoBack:true}" @on-click-back="goToPage" ref="header">点舞业绩</x-header>
    <div class="topHolder"></div>
    <card class="gridDisplay">
      <div class="card-padding" slot="content" style="padding-top: 0;">
        <div class="filterArea dateFilter" ref="filterArea">
          <group-title style="text-align: center;">当前显示日期：{{choosenDate}}<br/></group-title>
          <x-button :show-loading="loadingStatus" :disabled="loadingStatus" @click.native="changeDate(1)" class="inlineBtn">今日</x-button>
          <x-button :show-loading="loadingStatus" :disabled="loadingStatus" @click.native="changeDate(2)" class="inlineBtn">昨日</x-button>
          <a v-if="loadingStatus" class="datePicker"><i class="iconfont icon-rilicalendar107"></i></a>
          <datetime v-else v-model="choosenDate" :start-date="startDate" :end-date="endDate" @on-confirm="changeDate(0)" class="datePicker">
            <i class="iconfont icon-rilicalendar107"></i>
          </datetime>
          <div class="filterItem"><x-button @click.native="showUsersByDepart = true" type="default" v-html="$refs.usersByDepart&&$refs.usersByDepart.getNameValues().replace(/ /g,' / ')+'<i class=\'iconfont icon-xiangxia2\'></i>'" :show-loading="loadingStatus" :disabled="loadingStatus" class="inlineBtn popPickerBtn"></x-button></div>
        </div>
        <popup-picker @on-change="refreshPage()" :show.sync="showUsersByDepart" :show-cell="false" :data="usersByDepart" :columns="2" v-model="choosenDepartAndUser" ref="usersByDepart" class="btn_popup-picker"></popup-picker>
      </div>
    </card>
    <group title="业绩列表：">
      <x-table :cell-bordered="false">
        <thead>
        <tr style="background-color: #F7F7F7">
          <th width="38%">所属部门-姓名</th>
          <th width="30%">点舞数量</th>
          <th width="32%">点舞金额</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="(item,index) in performanceInfo" :key="index">
          <td v-text="item.DEPARTMENT + '-' + item.NAME"></td>
          <td v-text="item.orderNum"></td>
          <td v-text="'¥ ' + formatCurrency(item.orderTotal) + '元'"></td>
        </tr>
        <tr v-if="performanceInfo.length <= 0">
          <td colspan="4">
            <div class="listNoItem">暂无业绩明细</div>
          </td>
        </tr>
        </tbody>
      </x-table>
    </group>
    <br/>
    <br/>
  </div>
</template>

<script>
  import { Group, XButton, Card, PopupPicker, Datetime, GroupTitle, XTable, numberComma, dateFormat } from 'vux'
  import {getUsersByDepart,getDancePerformance} from "../../api";
  import {message,toDecimal2} from "../../common/util";

  export default {
    name:'dancePerformance',
    data(){
      return{
        showHeaderBack:this.GLOBAL.showHeaderBack,
        loadingStatus:false,
        showUsersByDepart: false,
        choosenDepartAndUser:['allDepart','allPerson'],
        choosenDate:dateFormat(new Date(), 'YYYY-MM-DD'),
        startDate:'2019-01-01',
        endDate:dateFormat(new Date(), 'YYYY-MM-DD'),
        performanceInfo:[]
      }
    },
    components:{
      Group,
      XButton,
      Card,
      PopupPicker,
      Datetime,
      GroupTitle,
      XTable
    },
    computed:{
      usersByDepart:{
        get(){
          return this.$store.getters.getUsersByDepart_all
        },
        set(value){
          this.$store.commit('updateUsersByDepart_all', value)
        }
      }
    },
    methods:{
      goToPage(){
        this.$router.push({path:'/'})
      },
      formatCurrency(number){
        return numberComma(toDecimal2(number))
      },
      changeDate(value){
        console.log(value)
        switch (value) {
          case 1:this.choosenDate = dateFormat(new Date(), 'YYYY-MM-DD');break;
          case 2:this.choosenDate = dateFormat(new Date().setDate(new Date().getDate()-1), 'YYYY-MM-DD');break;
          default: break;
        }
        console.log(this.choosenDate)
        this.refreshPage()
      },
      refreshPage(){
        this.loadingStatus = true
        let reqParams = {
          choosenDate:this.choosenDate,
          choosenDepartAndUser: this.choosenDepartAndUser
        }
        getDancePerformance(reqParams).then((res)=>{
          if(res.status == 'success'){
            this.performanceInfo = res.data
          }else if(res.status == 'error') {
            message({message:res.msg,type:'error'})
            console.log('getDancePerformance error info:', res.msg)
          }
          this.loadingStatus = false
        }).catch((err)=>{console.log('getDancePerformance catch info:',err)})
      }
    },
    mounted(){
      return new Promise((resolve,reject)=>{
        if(this.usersByDepart.length <= 0)
          getUsersByDepart().then((res)=>{
            if(res.status == 'success'){
              this.usersByDepart = res.data
            }else if(res.status == 'error') {
              message({message:res.msg,type:'error'})
              console.log('getUsersByDepart error info:', res.msg)
            }
            resolve()
          }).catch((err)=>{console.log('getUsersByDepart catch info:',err)})
        else resolve()
      }).then(()=>{
        this.choosenDepartAndUser = ['allDepart','allPerson']
        this.refreshPage()
        this.$store.commit('updateLoadingStatus',{isLoading:false})
      })
    }
  }
</script>

<style lang="scss">
  .dancePerformance{
    .filterArea{
      &.dateFilter{
        margin-bottom: 0.5rem;
        .datePicker{
          float: right;padding: 0;
          &.weui-cell:before{border-top: none;}
          i{font-size: 2rem;color:#535353;}
        }
      }
      .filterItem{margin-top: 0.5rem;}
    }
    .gridDisplay{
      h2.title{
        line-height: 1.8rem;color: #636363;font-size:2rem;text-align: center;margin: 1rem 0;
        small{font-size:1rem;color: #999999;}
      }
      .data{display: inline-block;height:1.5rem;}
    }
  }
</style>
